This is just a proof of concept, so you’ll have to play around with it to see how/if it can work for your needs. Try adding this to the </head> tab of a theme:
<script type="text/discourse-plugin" version="0.8.18">
api.decorateWidget('header-contents:after', helper => {
return helper.rawHtml(`<gcse:search linkTarget="_self"></gcse:search>`);
});
</script>
(Edit: improved the answer a little after having more time to look at it
)