It's a novel idea, but it's not going to work in any practical sense.
Most of those foams take about 15 minutes to setup, and you'll have to come up with some way to fabricate a mold while you spray it. It will be about an hour before you could safely enter your foam sarcophagus, and if this is to be used in an emergency situation, that's probably not time you're going to want to stand around and wait for the stuff to cure.
From my experience using the stuff for sealing purposes, I think you'd be lucky to get a cubic foot (about 30 liters) out of a can. The best R-value I can find for Great Stuff brand spray foams is about 2-3. Compare that to something like an example sleeping pad. The linked pad has an R-value of 4.9, and an inflated volume of about 2 cubic feet. As we can all know, a single sleeping pad is not going to be enough to keep you warm at the freezing point, even if you use it as a blanket. You'd have to use 4 cans just to simulate the insulation capabilities of a typical sleeping pad. You'll need many, many more to complete your sleeping vessel.
Additionally, you're going to end up with a solid, bulky mass of foam that you are going to haul out, and the cured foam is not biodegradable and it will be like managing a Styrofoam cooler making a mess everywhere, as well as all the empty aerosol cans.
There might be other foams that could work, but you won't be able to get them from the hardware store, and they won't cost only 4 bucks/can.