Get raw (escaped) attribute value from DOM element [duplicate]

How do get a raw, escaped attribute value from a DOM element so it can be used directly as a selector?

For example:

const selector = test.dataset.mydata;
console.log(selector); //it's not HTML friendly anymore

// selector is unescaped and can't be used directly:
const el = document.querySelector(`[myattr="${selector}"]`);
<div id="test" data-mydata="{&quot;foo&quot;:&quot;bar&quot;,&quot;id&quot;:&quot;blah&quot;}"></div>

<div myattr="{&quot;foo&quot;:&quot;bar&quot;,&quot;id&quot;:&quot;blah&quot;}"></div>