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.