If you pick $g$ first, you can take your condition as a recursive definition of $f$. i.e.
def f(X):
If X is a single character
return base_case(X)
Write X = Y || Z
return g(f(Y), g(Z))
If you disallow the null string, the only requirement on $g$ is that it is associative:
$$ g(a, g(b,c)) = g(g(a,b), c) $$
If you allow the null string, then you also require there be an element $z$ such that $g(a,z) = a = g(z, a)$, and you define $f() = z$.
Some simple examples include
$$g(x,y) = x+y$$
and
$$ g((m, x), (n, y)) = (m+n, 2^m x + y) $$
The intent of the second example is for a hash function like
h = 0
For each character c in X:
h = 2h + f(c)
return h
where computing the hash of a concatenation requires knowledge of the length of the string. So I've paired the length with the hash. Or, if you prefer, it's for the hash function
h = 0
n = 0
For each character c in X:
h = 2h + f(c)
n = n + 1
return (n, h)
You might be better served by thinking harder about the context of your problem; e.g. if the things you need to take the hash of are reliably distributed across different nodes in a consistent fashion, then you can simply be the hash function to be any function that combines the values of any hash function computed on the individual nodes. Because if you got the same string again, it would be distributed in the same way, and you'd compute the same hash function the next time you tried.
The claim is relatively well-known and even true for the more general case, where $P(Y \text{ is invertible}) = 1$, i.e., $Y$ does not have to be uniform. We assume that $X$ and $Y$ are independent and $P(X=x)=p$ uniformly over all matrices $x$ of rank $r$.
A proof goes as follows. Let $z \in \mathbb{F}_q^{m\times n}$ be a matrix of rank $r$. Start with demarginalization to obtain
$$ P(Z=z)=\sum_{y} P(Y=y,Z=z) = \sum_{y} P(Z=z|Y=y)P(Y=y), $$
where the sum ranges over all invertible matrices $y$. Now, since $y$ is invertible, $z=xy$ if and only if $x=zy^{-1}$ and hence $P(Z=z|Y=y) = P(X=zy^{-1}|Y=y)$. Due to independence of $X$ and $Y$ we get $P(X=zy^{-1}|Y=y)=P(X=zy^{-1})=p$, as $zy^{-1}$ is a matrix of rank $r$. Consequently,
$$ P(Z=z) =
\sum_{y} p P(Y=y) =p\sum_{y} P(Y=y) = p, $$
which proves the claim.
Best Answer
There is a very simple algorithm which gives you a hash function, assuming you store your matrix entries with integers in $\{0,1,...,p-1\}$:
Also please note that the examples you provide are not really good hash functions. There are way too many collisions, which would give serious performance hits if used in e.g. a hash table. The example I provided above is a perfect hash function (i.e. an injective map from the $\mathbb{F}_p$ matrices to $\mathbb{Z}$), so no collisions can exist here (unless $n$ and $p$ are large enough to cause overflow, but even then the probability is very small).
I don't think one can do much better than this, since any good hash would take all matrix entries into account, and this only does one addition and one multiplication per entry. If this is still not working for you, chances are that you don't just need a hash.