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.