What's the reason a military/reconaissance plane would have a light on top of its roof? The SR-71 was the highest-flying airbreathing plane, after all. There was little danger from any collision from above.
That is an anti-collision light as described in the flight manual:
Anti-Collision/Fuselage Lights and Switch
Two combination retractable anti-collision and fuselage lights are located at the top and bottom of the fuselage, near the middle. The lights are controlled by a three-position toggle switch on the pilot's lighting panel. The switch positions are: ANTI COLLISION (forward), FUS (aft), and OFF (center). In ANTI COLLISION, the lights extend, illuminate red, and rotate at 45 rpm (which produces 90 flashes per minute).
The US AIM 4-3-23 recommends the following (see this answer for more details):
In addition, aircraft equipped with an anti-collision light system are required to operate that light system during all types of operations (day and night).
The idea is that the ground crew and other aircraft on the ground are alerted that the engines are running (or about to be started) or that the aircraft is moving (or about to move). The ground crew can see the lower light (bottom of fuselage), while other aircraft can see the upper one (top of fuselage).
In flight, the SR-71 would switch to FUS or OFF mode:
In FUS (fuselage), the lights are retracted and illuminate white. The lights are rectracted and off when the switch is in OFF.
During missions the SR-71 had to be refuelled air to air many times. These missions took place also at night, so visual cues were absolutely necessary.
Also the mission obviously were often flown lower and slower in "civillian" airspace for departure and approach legs, so lights were there for general safety also.
At high altitudes they were of course not necessary.