Hello,
If you want to make changes in the theme files then copy header.php file to child theme and edit that according to your needs. Don’t make any changes directly in parent theme files because you will lose them after the theme update. Use child theme for any additional customization. But you need to be sure that mentioned code works in general. Did you check if that code works for you with any default WP theme like Storefront or Twenty Twenty-One?
Regards