However, what happens if a miner builds a block containing this transaction? I don't see how he can avoid including this in his stored blockchain.
A Bitcoin node can actually refuse to relay a transaction with illegal content or even because of low fees ( miners do it all the time) by not adding it to its mempool which actually means he won't be broadcasting it to other nodes it's kinda like a local choice that limits the propagation of that transaction.
However it doesn't really prevent a miner from including that same transaction in a block. If a miner receives the transaction through another node and successfully mines a block with it they will broadcast this new block to the network as long as the transaction and the entire block adhere to Bitcoin's consensus rules such as valid signatures and proof of work a node has no choice but to accept and store the block in its copy of the blockchain regardless of the content of the transaction in order to stay in sync with the rest of the network.