Including the dark mode toggle in the header of your HTML page is absolutely appropriate and a common practice. Here's why, along with some considerations to ensure it works well:
Why the Header is a Good Choice
User Accessibility:
The header is a logical place for UI controls that affect the entire site (like theme switches).
Users expect global settings to be easily accessible, and the header is a natural location for such controls.
Consistency:
If your site uses a consistent header template across all pages, placing the toggle there ensures it’s available everywhere without duplication.
Visual Hierarchy:
The header already contains critical elements (title, navigation), so adding a theme toggle maintains a logical flow of user controls.
Code Improvements/Considerations
Your current implementation is solid, but here are a few refinements to make it even better:
While the header is great, you might also consider:
Placing it in a settings menu (if your site has one).
Adding it to the footer for a "hidden" but accessible option.
Using a floating button (e.g., in the corner).
But the header is perfectly fine for most use cases!
Final Thoughts
Your implementation is already solid. The key improvements are ensuring accessibility, styling it to fit your design, and adding functional JavaScript to toggle themes. The header is a logical and user-friendly location for this control.