Yes quotename is a nice way of doing things, but bear in mind it has a limit of
nvarchar(258)
so you probably are better off using a UDF so you don't get caught out working with a really really big string
Nice method using the variable though, although perhaps a little @q is less intrusive? Using a substitute char also makes good sense, like a ~ character or something not often used...