Why did the sun get hot?

I stand corrected.

When a gas is allowed to expand, it’s pushing against its surroundings, Those surroundings may be the atmosphere, or maybe the piston of an engine. In either case, they do mechanical work via that pushing: the weighty atmosphere gets lifted upward just a tiny bit, or the piston drives a crankshaft that pushes your car forward.

Your can of “compressed air”, by your description, sounds like Dust Off, or something similar. This is more complicated, and doesn’t have quite as much to do with mechanical work. The can is filled with a substance, some of which is a liquid and some of which is a gas, all stored under pressure. When you squeeze the valve, the gas escapes, and the liquid begins to boil, creating more gas. This is essentially evaporative cooling: when the pressure inside the can is decreased, the hottest molecules of liquid transform into a gas and escape through the valve, leaving the coldest molecules of the liquid behind. Keep spraying, and eventually the liquid gets so cold that there aren’t very many molecules of it that are hot enough to become gas anymore - and the jet of escaping gas becomes less intense.

For a more clear-cut and simple case of expanding gas cooling off, take a filled car tire and remove the valve core. Come back in thirty seconds when the air has escaped completely; you’ll find that the valve stem is very cold to the touch. In some cases, it may even develop condensation or frost on the outside of it.