Headlights go on and off when engine is hot

The headlight switch does have built in protection that will cause exactly what you describe but...
If the headlight switch itself is the fault, it would occur without engine running. Put a good battery charger or jumper cables from another vehicle on and turn the lights on.

I was thinking along the same lines myself, that it maybe the thermal trip in the light switch.
We have this issue quite a lot with 60s US cars here in Germany. German traffic regulations require sealed beams to be removed & H4 headlights to be fitted. Due to the increased wattage of the H4s, more current has to flow, thus tripping the thermal protection in the light switch. We fit 2 relays in the headlight circuitry. 1 for dipped beam & 1 for high beam. Problem solved.