The problem is that your edit command,
exe a:cmd '[Scratch' . g:scratchbuf_no . ']'
expands to this, when g:scratchbuf_no is 1, for example:
edit [Scratch1]
Vim looks for a file matching the glob "[Scratch1]", which is
a single character in the set "S c r a t c h 1". If the glob
doesn't match any files, the buffer name "[Scratch1]" is used. If
the glob matches one file, that file is opened. If you happen to
have more than one file with a single-character name from that set,
then that glob will match more that one file and you will get E77.
You can fix that problem by escaping the brackets like this:
exe a:cmd '\[Scratch' . g:scratchbuf_no . '\]'
Some years ago I wrote a similar function to create a scratchpad
buffer with a name enclosed in brackets with results similar to
yours. I was very confused until the "Aha!" moment.
Regards,
Gary