It doesn't work because shell doens't do that. It doesn't evaluate variables inside variables inside variables inside variables -- and that's mostly a good thing, you could tie yourself in knots with that.
Thanks for the reply and sorry to bother you again.
I am pretty new to this and I am afraid I didn't understand the how to use the file that is modified. Please explain. I have changed the content of file to
$cat test
cat <<EOF
<td>$test</td>
<h2>$test2</h2>
EOF
But I am not sure how to use this in the script to have the variable values replaced.