A cost that not many people consider -- on any major expenditure, not in any way limited to solar panels -- is opportunity cost.
As in, at 8% a year on average, you could earn $3,200 a year if you put the $40K in the stock market as opposed to something else.
So (ignoring tax impact for a minute) the real cost for the example at hand is not only the $40K initial one-time outlay, but an additional $3.2K per year, every year, in foregone income.
Then the tax considerations come in. Their net effect varies greatly from person to person depending on your specific tax situation and can get complicated.
If you get a $12K income tax credit, the net one time cost becomes $28K. If that results in an additional $12K refund on top of what you would have otherwise gotten, and if you invest it, you still have a net $2,240 opportunity cost ($28K net investment x 8% foregone earnings).
If you would have paid more than $12K in federal taxes otherwise, and now have a $12K tax reduction, you'd need to invest the $12K you would have paid in taxes in order to reap the same partial offset. If you don't do that, the opportunity cost reverts to the original $3,200.
In short, the true calculation isn't simple, and you might want to consult a tax advisor to arrive at the number that applies to your specific circumstances.
I think the technology will eventually get there. But right now, the large upfront outlay makes it really hard for the numbers to work.
You might still do it for other reasons. A pro who knows your personal situation can help you determine your true all-in cost. Once you know that, you can make an informed decision.
Late Add: This question is a great example of the mathematical problem facing a lot of green energy -- primarily solar and wind.
With a traditional generation plant, the largest expense is fuel. And it happens on a pay-as-you-go basis. IOW, the upfront cost of the plant is relatively small. The main cost of the electricity is the ongoing expense of running the thing.
With solar and wind, the fuel is free. The issue is the nasty initial cost....which has to be funded 100% upfront before a single watt of electricity is generated. And, as detailed above, the opportunity cost of the money on top of the initial outlay.
There's also the fact that if it's cloudy or raining or the wind's not blowing sufficiently, there's no power being generated....even though the clock on the cost of capital needed to construct the plant ticks 60/60/24/7/365 no matter what the weather's doing.
It's why solar and wind are suited to only parts of the country. At least for now. I have no doubt that technology will eventually reduce the upfront outlay for solar or wind to the point that the all-in cost (including opportunity cost) is justified. But I'm 66, and progress is painfully slow. Whether that happens before I'm in an urn on Mrs. Basket Case's bookshelf, I don't know. Right now, I'd have to bet not.