This sounds like an electrical issue. It's not uncommon for an electrical component to fail in this situation. You are driving the car, the electrical part get's very hot but works OK, then you stop and turn off the car and then the electrical component fails.
The a/c compressor may be losing the power signal due to a defective a/c relay or a/c compressor clutch may be getting power but not energizing. Have it checked out to know for sure.