Applescript "expected then but found identifier"
Before you try running a non-trivial script using osascript, first try compiling it (and running it, if feasible) in Script Editor.

When I compiled your script in Script Editor, it revealed that your script is trying to use the term custom title—a Terminal-specific term—without targeting Terminal, so the term isn't available. Extend your first line, tell … to …, into a tell block:

tell application "Terminal Latest"
    activate

    set allWindows to number of windows
    set newTab to true

    repeat with i from 1 to allWindows
        set allTabs to number of tabs of window i
        repeat with j from 1 to allTabs
            if custom title of tab j of window i contains "Hi" then
                set frontmost of window i to true
                set selected of tab j of window i to true
                set newTab to false
            end if
        end repeat
    end repeat
end tell

if newTab then
    tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
    tell application "Terminal Latest"
        do script ("tabname $2") in selected tab of the front window
        set custom title of selected tab of the front window to "$2"
    end tell
end if

tell application "Terminal Latest" to do script ("$1") in selected tab of the front window


But you're going to find that this code still doesn't work, because you're trying to use shell variables $1 and $2 inside single-quoted strings, where the shell won't interpret them. So you'll also need to break up the quoting of those lines, e.g., in your osascript command arguments change

-e 'do script ("tabname $2") in selected tab of the front window'


to

-e 'do script "tabname '"$2"'" in selected tab of the front window'


[Untested.]

Also, the parentheses were unnecessary.

However, if you're storing this in a shebang script file, you don't need to use a shell to run osascript. You can change the first line to

#!/usr/bin/osascript


and then just put your AppleScript code after that, without the -e options, surrounding single-quoting or line-continuation backslashes.

Finally, since AppleScript supports the hash # comment character, you can edit, compile and run (as appropriate) osascript shebang scripts in Script Editor.

Update: A minute later I realized you want to pass command-line arguments to the script. So, you do want to use a shell to run osascript for your case. However, you can simplify things by using a Here document to place the script inline:

osascript&lt;&lt;EOF
    &lt;your script here&gt;
EOF


Variables/arguments will be expanded.