Another option is to turn this fragment into a “virtual DOM tree” in memory, using createContextualFragment. Doing this, it doesn’t need to be in the DOM to use querySelector, for example.
We can accomplish much the same result using document.createElement, creating a root element and populating that as we need, but from string to DOM, i like the first approach.
It is an advanced API, not something to be introduced early in the curriculum, but one of the most useful DOM tools I’ve seen. I use it all the time.