You're running into an "order of operations" type problem I think. The
< is processed before the commands are execute, so you're running something similar (though not identical to)
varname=$varname test2.sh | ssh firstname.lastname@example.org bash
which runs the
test2.sh locally, then sends that output to
bash on the remote server. That's why you see the output of
test2.sh but then get an error when it tries to execute that output as a command.
If you're running a script that's executable you don't really need the
bash there though, so you could just do
ssh email@example.com "varname=$varname ./test2.sh"
test2.sh is available on the remote server.
If you don't have
test2.sh on the remote server and still want to execute it there you could do it like so:
ssh firstname.lastname@example.org varname="$varname" bash < test2.sh
which will redirect the script source but do the variable processing in the context of the