As I noted above, it is quite common to find grammatical inconsistencies in Psalms and other poetic Biblical sections. Switching back and forth between the second and third person forms in reference to God is probably the most common example. So much so that I would be more surprised if a (God-related) psalm didn't include this phenomenon than if it did. As noted in this book (p. 326), "in Hebrew, as in any language, a writer enjoys a certain grammatical freedom. In poetry, the choice of a particular form may not always be dictated by grammatical considerations, but, for instance, by some metrical necessity."
Let's go through a few of the opening psalms to get a feel for this concept with some examples.
Psalm 3
This one starts off referring to God in the second person.
ב יְהוָה מָה רַבּוּ צָרָי רַבִּים קָמִים עָלָי. ג רַבִּים אֹמְרִים לְנַפְשִׁי אֵין יְשׁוּעָתָה לּוֹ בֵאלֹהִים סֶלָה. ד וְאַתָּה יְהוָה מָגֵן בַּעֲדִי כְּבוֹדִי וּמֵרִים רֹאשִׁי.
Switch to third person in verse 5:
קוֹלִי אֶל יְהוָה אֶקְרָא וַיַּעֲנֵנִי מֵהַר קָדְשׁוֹ סֶלָה
Back to second person in verse 8:
קוּמָה יְהוָה הוֹשִׁיעֵנִי אֱלֹהַי כִּי הִכִּיתָ אֶת כָּל אֹיְבַי לֶחִי שִׁנֵּי רְשָׁעִים שִׁבַּרְתָּ
Verse 9, as I've noted elsewhere, sums it up with both third and second person references:
לַיהוָה הַיְשׁוּעָה עַל עַמְּךָ בִרְכָתֶךָ סֶּלָה
Psalm 4
Again, we start off with second person:
ב בְּקָרְאִי עֲנֵנִי אֱלֹהֵי צִדְקִי בַּצָּר הִרְחַבְתָּ לִּי חָנֵּנִי וּשְׁמַע תְּפִלָּתִי
Then, third person for v. 4-6*:
ד וּדְעוּ כִּי הִפְלָה יְהוָה חָסִיד לוֹ יְהוָה יִשְׁמַע בְּקָרְאִי אֵלָיו. ה רִגְזוּ וְאַל תֶּחֱטָאוּ אִמְרוּ בִלְבַבְכֶם עַל מִשְׁכַּבְכֶם וְדֹמּוּ סֶלָה. ו זִבְחוּ זִבְחֵי צֶדֶק וּבִטְחוּ אֶל יְהוָה
Back to second person for the remainder of the psalm:
ז רַבִּים אֹמְרִים מִי יַרְאֵנוּ טוֹב נְסָה עָלֵינוּ אוֹר פָּנֶיךָ יְהוָה. ח נָתַתָּה שִׂמְחָה בְלִבִּי מֵעֵת דְּגָנָם וְתִירוֹשָׁם רָבּוּ. ט בְּשָׁלוֹם יַחְדָּו אֶשְׁכְּבָה וְאִישָׁן כִּי אַתָּה יְהוָה לְבָדָד לָבֶטַח תּוֹשִׁיבֵנִי
Psalm 5
This entire psalm refers to God in the second person with one exception. Verse 7 reads:
תְּאַבֵּד דֹּבְרֵי כָזָב / אִישׁ דָּמִים וּמִרְמָה יְתָעֵב יְהוָה
This is a parallelism in which the first part is written in second person form, and the second part in the third person form.
Psalm 6
Here again we start with second person, then shift to third person at the end for v. 9-10:
ט סוּרוּ מִמֶּנִּי כָּל פֹּעֲלֵי אָוֶן כִּי שָׁמַע יְהוָה קוֹל בִּכְיִי. י שָׁמַע יְהוָה תְּחִנָּתִי יְהוָה תְּפִלָּתִי יִקָּח. יא יֵבֹשׁוּ וְיִבָּהֲלוּ מְאֹד כָּל אֹיְבָי יָשֻׁבוּ יֵבֹשׁוּ רָגַע
Psalm 7
Start with second person for v. 2-8:
ב יְהוָה אֱלֹהַי בְּךָ חָסִיתִי הוֹשִׁיעֵנִי מִכָּל רֹדְפַי וְהַצִּילֵנִי. ג פֶּן יִטְרֹף כְּאַרְיֵה נַפְשִׁי פֹּרֵק וְאֵין מַצִּיל. ד יְהוָה אֱלֹהַי אִם עָשִׂיתִי זֹאת אִם יֶשׁ עָוֶל בְּכַפָּי. ה אִם גָּמַלְתִּי שׁוֹלְמִי רָע וָאֲחַלְּצָה צוֹרְרִי רֵיקָם. ו יִרַדֹּף אוֹיֵב נַפְשִׁי וְיַשֵּׂג וְיִרְמֹס לָאָרֶץ חַיָּי וּכְבוֹדִי לֶעָפָר יַשְׁכֵּן סֶלָה. ז קוּמָה יְהוָה בְּאַפֶּךָ הִנָּשֵׂא בְּעַבְרוֹת צוֹרְרָי וְעוּרָה אֵלַי מִשְׁפָּט צִוִּיתָ. ח וַעֲדַת לְאֻמִּים תְּסוֹבְבֶךָּ וְעָלֶיהָ לַמָּרוֹם שׁוּבָה
Verses 9 and 10 are arguable as to their translation:
ט יְהוָה יָדִין עַמִּים שָׁפְטֵנִי יְהוָה כְּצִדְקִי וּכְתֻמִּי עָלָי. י יִגְמָר נָא רַע רְשָׁעִים וּתְכוֹנֵן צַדִּיק וּבֹחֵן לִבּוֹת וּכְלָיוֹת אֱלֹהִים צַדִּיק
"שָׁפְטֵנִי" is an imperative, which implies second person. "יָדִין עַמִּים" implies third person, but Mechon-Mamre translates "O LORD, who ministerest judgment". "יִגְמָר נָא" is probably modifying "רַע" instead of God. "וּתְכוֹנֵן צַדִּיק" is second person form; "וּבֹחֵן לִבּוֹת וּכְלָיוֹת אֱלֹהִים צַדִּיק" can go both ways in my mind.
But certainly by v.11 we are in full-fledged third person mode:
יא מָגִנִּי עַל אֱלֹהִים מוֹשִׁיעַ יִשְׁרֵי לֵב. יב אֱלֹהִים שׁוֹפֵט צַדִּיק וְאֵל זֹעֵם בְּכָל יוֹם. יג אִם לֹא יָשׁוּב חַרְבּוֹ יִלְטוֹשׁ קַשְׁתּוֹ דָרַךְ וַיְכוֹנְנֶהָ
This continues through the end of the psalm.
By now we start to get the idea. Can we come up with rigid contextual rules for when God will be addressed in the third person and when in the second person? Maybe, but I doubt it. I would not be opposed to drash explanations that would try to explain for certain occurrences of this phenomenon, but it would be just that: drash.
* This example is more understandable, since the psalmist begins by talking to God, thus using the second person, then switches to address the "sons of man" in v. 3-6, which explains the shift to third person for God. The same is true below in Psalm 6 where the shift occurs as the psalmist begins to address the "workers of inquity".