You can get a subset of characters from a variable easily in the shell. The below works in /bin/bash and /bin/zsh and probably others as well, but I haven't tested beyond those.
% name="ABCDE1234567890"
% echo "${name:0:2}"
% AB
Using a similar process in the shell, you can also get the last x characters of a given string, which means you can add those to a new "prefix" string to come up with a new computer name. The syntax looks like this. This grabs the last 10 characters of the same example string:
% name="ABCDE1234567890"
% echo "${name: -10}"
% 1234567890
So...
#!/bin/zsh
## Get the existing computer name
computer_name=$(scutil --get ComputerName)
echo "Current computer name is $computer_name"
## Get the number of characters in the computer name
str_len="${#computer_name}"
echo "Computer Name length is: $str_len"
## Create a new string length, minus 2
len_minus_two=$((str_len-2))
## Get the last x number of characters from the computer name. This is our "suffix"
comp_name_end="${computer_name: -$len_minus_two}"
echo "Last $len_minus_two characters of the computer name are: $comp_name_end"
## Set the new characters for the start of the computer name here
new_prefix="XY"
## Set new computer name, combining new prefix with our suffix
new_comp_name="${new_prefix}${comp_name_end}"
echo "New computer name will be: $new_comp_name"
## Set the new computer name
scutil --set ComputerName "$new_comp_name"
scutil --set LocalHostName "$new_comp_name"